/**
 * Created by john on 2018/1/10.
 */
$(function () {
    $('.newsdetail_comment').click(function (e) {
        e.stopPropagation();

        location.href = '#commit111'

        $("section.comment_textarea").removeClass("hide");

        $('section.comment_textarea .cancel').click(function () {
            $("section.comment_textarea").addClass("hide")
        })
    });


    var Request = new Object();
    Request = GetRequest();
    //变量声明
    var comment_num = null
    var like_num = null

//函数调用
    $.showLoading()

    $.ajax({
        url: connect + 'index/articleContent',
        data: {mstr: mstr, model_id: Request.model_id, article_id: Request.article_id},
        type: 'post',
        success: function (data) {
            $.hideLoading()
            console.log(data);
            var data = data.data
            //ͷ头部
            $('header.newsdetail h4').text(data.title)
            $('head title').text(data.title)

            $('.article_head .article_time').text(getTime(data.update_time))
            $('.article_head .article_liulan').text(' ' + data.view)
            $('.article_head .article_liulan').addClass('icon-liulan')

            //文章内容
            $('section pre').html(data.content)

            //点赞 收藏
            if (data.is_pviews == 0) {
                $('.newsdetail_dianzan > div').eq(0).css({'color': '#444', borderColor: '#444'})
            } else {
                $('.newsdetail_dianzan > div').eq(0).css({'color': '#fd5454', borderColor: '#fd5454'})
            }

            if (data.is_collect == 0) {
                $('.newsdetail_dianzan > div').eq(1).css({'color': '#444', borderColor: '#444'})
            } else {
                $('.newsdetail_dianzan > div').eq(1).css({'color': '#ffbb00', borderColor: '#ffbb00'})
            }

            like_num = data.like_num
            comment_num = data.comment_num
            likeNum()

            clickZan() //点赞函数
            submitComment() //提交评论
            getComment(1)   //获取评论
            getZan()        //获赞
        }
    })

    function likeNum() {
        $('.newsdetailTab_pinglun .weui-navbar > a').eq(0).find('span').text(comment_num)
        $('.newsdetailTab_pinglun .weui-navbar > a').eq(1).find('span').text(like_num)
        $('.newsdetail_comment .icon-41').text('  ' + comment_num)

    }

    function clickZan() {
        $('.newsdetail_dianzan > div').eq(0).on('click', function () {
            if ($('.newsdetail_dianzan > div').eq(0).css('color') == 'rgb(68, 68, 68)') {
                $('.newsdetail_dianzan > div').eq(0).css({'color': '#fd5454', borderColor: '#fd5454'})
                $.toptip('点赞+1', 'success')
                like_num++
                likeNum()
            } else {
                $('.newsdetail_dianzan > div').eq(0).css({'color': '#444444', borderColor: '#444444'})
                $.toptip('点赞-1', 'success')
                like_num--
                likeNum()
            }

            $.ajax({
                url:connect + 'index/articlePviews',
                type:'post',
                data:{mstr:mstr,model_id:Request.model_id,article_id:Request.article_id},
                success:function(data){
                    getZan()
                }
            })


        })

        $('.newsdetail_dianzan > div').eq(1).click(function () {
            if ($('.newsdetail_dianzan > div').eq(1).css('color') == 'rgb(68, 68, 68)') {
                $('.newsdetail_dianzan > div').eq(1).css({'color': '#ffbb00', borderColor: '#ffbb00'})
                $.toptip('收藏成功', 'success')
            } else {
                $('.newsdetail_dianzan > div').eq(1).css({'color': '#444444', borderColor: '#444444'})
                $.toptip('取消收藏', 'success')
            }

            $.ajax({
                url:connect + 'index/articleCollect',
                type:'post',
                data:{mstr:mstr,model_id:Request.model_id,article_id:Request.article_id},
                success:function(data){
                }
            })
        })

    }

    //评论提交
    function submitComment() {
        $('.controll_comment .submit').on('click', function () {
            var neirong = $('.comment_textarea textarea.weui-textarea').val()

            $.ajax({
                url: connect + 'index/commentArticle',
                data: {mstr: mstr, model_id: Request.model_id, article_id: Request.article_id, content: neirong},
                type: 'post',
                success: function (data) {
                    $.toptip('评论成功', 'success');
                    console.log(data);
                    getComment()
                    $("section.comment_textarea").addClass("hide")
                    location.href = '#tab1'
                    comment_num++
                    likeNum()
                }
            })

        })
    }

    //评论获取
    function getComment(page){
        $.ajax({
            url:connect + 'index/articleComment',
            data:{mstr:mstr,model_id:Request.model_id,article_id:Request.article_id,page:page,pagesize:50},
            type:'post',
            success:function(data){
                data.connect = connectImg

                if(data.data == undefined) {
                    $('#tab2').html(' ')
                    return
                }

                $.each(data.data,function(i,e){
                    e.create_time = getFullTime(e.create_time)
                })
                var html = template('comment2',data)
                $("#tab1").html(html)
            }
        })
    }

    //点赞获取
    function getZan(){
        $.ajax({
            url:connect + 'index/articlePviewsCrew',
            data:{mstr:mstr,model_id:Request.model_id,article_id:Request.article_id,pagesize:100},
            type:'post',
            success:function(data){
                console.log(data);
                if(data.data == undefined) {
                    $('#tab2').html(' ')
                    return
                }

                data.connect = connectImg
                $.each(data.data,function(i,e){
                    e.create_time = getFullTime(e.create_time)
                })
                var html = template('dianzan1',data)
                $('#tab2').html(html)
            }
        })
    }

})